package cn.itcast.bos.action.base;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.apache.commons.lang.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionChainResult;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

import cn.itcast.bos.action.common.BaseAction;
import cn.itcast.bos.domain.base.Area;
import cn.itcast.bos.service.base.IAreaService;
import cn.itcast.bos.utils.PinYin4jUtils;
@Controller
@Namespace("/")
@ParentPackage("json-default")
@Scope("prototype")
public class AreaAction extends BaseAction<Area> {
	
	private static final long serialVersionUID = -2376277815373583226L;
	
	@Autowired
	private IAreaService areaService;
	
	private File file;	

	public void setFile(File file) {
		this.file = file;
	}
	
	/**
	 * 一键上传,POI读取Execl文件
	 * @return
	 * @throws IOException
	 */
	@Action(value="area_batchImport",results={@Result(name="none",location="/pages/base/area.html",type="redirect")})
	public String areaBatchImport() throws IOException{
		// 获得HSSFWork
		HSSFWorkbook workbook = new HSSFWorkbook(new FileInputStream(file));
		// 获取工作簿sheet
		HSSFSheet sheetAt = workbook.getSheetAt(0);
		ArrayList<Area> areas = new ArrayList<>();
		// 获取工作簿中每行数据
		for (Row row : sheetAt) {
			// 第一行标题跳过
			if (row.getRowNum() == 0) {
				continue;
			}
			// 跳过空行
			if (row.getCell(0) == null || StringUtils.isBlank(row.getCell(0).getStringCellValue())) {
				continue;	
			}
			// 构建对象中的数据
			Area area = new Area();
			area.setId(row.getCell(0).getStringCellValue());
			area.setProvince(row.getCell(1).getStringCellValue());
			area.setCity(row.getCell(2).getStringCellValue());
			area.setDistrict(row.getCell(3).getStringCellValue());
			area.setPostcode(row.getCell(4).getStringCellValue());
			// 城市编码
			String city = area.getCity().substring(0,area.getCity().length()-1);
			String	cityCode = PinYin4jUtils.hanziToPinyin(city,"");
			area.setCitycode(cityCode);
			System.out.println(cityCode);
			
			//简单码
			String province = area.getProvince().substring(0,area.getProvince().length()-1);	// 省
			String district = area.getDistrict().substring(0,area.getDistrict().length()-1);	// 区
			
			String[] headByString = PinYin4jUtils.getHeadByString(province+district);
			StringBuilder sb = new StringBuilder();
			for (String s : headByString) {
				sb.append(s);
			}
			System.out.println(sb);
			area.setShortcode(sb.toString());
			
			// 调用service保存数据
			areas.add(area);
		}
		areaService.save(areas);
		return NONE;
	}
	
	/**
	 * 分页显示所有区域
	 * @return
	 */
	@Action(value="area_page_query",results={@Result(name="none",type="json")})
	public String areaPageQuery(){
		Pageable pageable = new PageRequest(page-1,rows);
		
		Specification<Area> specification = new Specification<Area>() {
			// 构建查询条件
			/**
			 * Root：保存泛型中的所有属性
			 * CriteriaQuery：构建简单查询条件
			 * CriteriaBuilder：构建复杂查询条件
			 */
			@Override
			public Predicate toPredicate(Root<Area> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				ArrayList<Predicate> list = new ArrayList<>();
				// 市查询条件
				if (StringUtils.isNotBlank(model.getCity())) {
					Predicate areaCityWhere = cb.like(root.get("city").as(String.class), "%"+model.getCity()+"%");
					list.add(areaCityWhere);
				}
				// 省查询条件
				if (StringUtils.isNotBlank(model.getProvince())) {
					Predicate areaPrivenceWhere = cb.like(root.get("province").as(String.class), "%"+model.getProvince()+"%");
					list.add(areaPrivenceWhere);
				}
				// 区查询条件
				if (StringUtils.isNotBlank(model.getDistrict())) {
					Predicate areaDistrictWhere = cb.like(root.get("district").as(String.class), "%"+model.getDistrict()+"%");
					list.add(areaDistrictWhere);
				}
				return cb.and(list.toArray(new Predicate[0]));
			}

		};
		Page<Area> areas = areaService.findPageAll(specification, pageable);
		// 压入值栈
		pushPageDateToValueStack(areas);
		return NONE;
	}

	
}
